<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 上面的小按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="mini" @click="addPermission(1, '0')">添加权限</el-button>
        </template>
      </page-tools>

      <!-- 权限数据 -->
      <el-table :data="List" row-key="id">
        <el-table-column label="名称" header-align="center" align="center" prop="name" />
        <el-table-column label="标识" header-align="center" align="center" prop="code" />
        <el-table-column label="描述" header-align="center" align="center" prop="description" />
        <el-table-column label="操作" header-align="center" align="center">
          <template v-slot="{row}">
            <!-- 这里的插槽无须具名 -->
            <el-button v-if="row.type === 1" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加弹层 -->
      <el-dialog :visible.sync="showDialog" :title="showText" @close="btnCancel">
        <el-form ref="permiss" label-width="120px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>

          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>

          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>

          <el-form-item label="开启" prop="enVisible">
            <el-switch v-model="formData.enVisible" />
          </el-form-item>
        </el-form>

        <template v-slot:footer>
          <el-row type="flex" justify="center">
            <el-col :span="6">
              <el-button @click="btnCancel">取消</el-button>
              <el-button type="primary" @click="btnOk">确定</el-button>
            </el-col>
          </el-row>
        </template>

      </el-dialog>
    </div>
  </div>
</template>>

<script>
import {
  getPermissionListAPI,
  delPermissionAPI,
  getPermissionDetailAPI,
  addPermissionAPI,
  updatePermissionAPI
} from '@/api'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      List: [], // 表格权限数据
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      showDialog: false, // 新增和编辑的弹层显示
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '添加'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      // 使用转化树形函数 根节点是谁？ 是空字符？而是'0'
      this.List = transListToTreeData(await getPermissionListAPI(), '0')
      console.log(this.List)
    },

    // 删除权限
    async delPermission(id) {
      await this.$confirm('确认删除嘛?')
      await delPermissionAPI(id)
      this.$message.success('删除权限节点成功')
      this.getPermissionList()
    },
    // 增加权限
    addPermission(type, id) {
      this.formData.type = type
      this.formData.pid = id
      this.showDialog = true
    },

    // 编辑权限
    async editPermission(id) {
      this.formData = await getPermissionDetailAPI(id)
      this.showDialog = true
    },

    // 点击确定
    btnOk() {
      this.$refs.permiss.validate().then(() => {
        if (this.formData.id) {
          return updatePermissionAPI(this.formData)
        }
        return addPermissionAPI(this.formData)
      }).then(() => {
        this.$message.success('操作权限成功')
        this.getPermissionList()
        this.showDialog = false
      })
    },
    // 点击取消
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.$refs.permiss.resetFields()
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
